package com.lambda.springbootlambda.stream;

import com.lambda.springbootlambda.construct.User;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 * @author ytj
 * @date 2021/12/15
 */
public class StreamTest {
    public static void main(String[] args) {
        List<User> list= Arrays.asList(
                new User("1","张三","1"),
                new User("2","李四","1"),
                new User("3","王五","2"),
                new User("4","赵六","2"),
                new User("5","麻7","3"));
        StreamTest.list(list);
        System.out.println("=================================");
        StreamTest.set(list);
        System.out.println("=================================");
        StreamTest.map(list);
        System.out.println("=================================");
        StreamTest.groupby(list);
        System.out.println("=================================");
        StreamTest.joins(list);
        System.out.println("=================================");
        StreamTest.screen(list);
        System.out.println("=================================");
        StreamTest.moreScreen(list);
        System.out.println("=================================");
        StreamTest.peek(list);
        System.out.println("=================================");
        StreamTest.distinct();
        System.out.println("=================================");
        StreamTest.limit(list);
        System.out.println("=================================");
        StreamTest.sortByRule(list);
        System.out.println("=================================");
        StreamTest.allMatch(list);
        System.out.println("=================================");
        StreamTest.anyMatch(list);
        System.out.println("=================================");
        StreamTest.noMatch(list);
        System.out.println("=================================");
        StreamTest.findFirst(list);
        System.out.println("=================================");
        StreamTest.findAny(list);
        System.out.println("=================================");
        StreamTest.count(list);
        System.out.println("=================================");
        StreamTest.max(list);
        System.out.println("=================================");
        StreamTest.min(list);
    }

    public static void list(List<User> list){
        List<User> changeList=list.stream().collect(Collectors.toList());
        changeList.forEach(System.out::println);
    }

    public static void set(List<User> list){
        Set<User> set=list.stream().collect(Collectors.toSet());
        set.forEach(System.out::println);
    }

    /**
     * key值不能重复，重复会报错
     * */
    public static void map(List<User> list){
        Map<String, User> collect = list.stream().collect(Collectors.toMap(User::getId, User->User));
        collect.forEach((k,v)-> System.out.println("key:"+k+",value:"+v));
    }

    /**
     * 分组
     * */
    public static void groupby(List<User> list){
        Map<String, List<User>> collect = list.stream().collect(Collectors.groupingBy(User::getClassName));
        collect.forEach((k,v)->{
            System.out.println("k:"+k);
            v.forEach(System.out::println);
        });
    }

    /**
     * 拼接字符串
     * */
    public static void joins(List<User> list){
        String collect = list.stream().map(User::getName).collect(Collectors.joining(",","[","]"));
        System.out.println(collect);
    }

    /**
     * 条件筛选
     * */
    public static void screen(List<User> list){
        Map<String, User> collect = list.stream().filter(user -> !"1".equals(user.getId())).collect(Collectors.toMap(User::getId, User -> User));
        collect.forEach((k,v)->{
            System.out.println("k:"+k+",v:"+v);
        });
    }

    /**
     * 多个集合筛选
     * */
    public static void moreScreen(List<User> list){
        List<User> list1=Arrays.asList(
                new User("6","张三1","3"),
                new User("7","张三2","3"),
                new User("8","张三3","3"),
                new User("9","张三4","3")
        );
        Map<String, List<User>> collect = Stream.of(list, list1)
                .flatMap(users -> users.stream().filter(e -> !"1".equals(e.getClassName())))
                .collect(Collectors.groupingBy(User::getClassName));
        collect.forEach((k,v)->{
            System.out.println("k:"+k);
            v.forEach(System.out::println);
        });
    }

    /**
     * 操作流中数据
     * 不会截断可以继续操作
     * */
    public static void peek(List<User> list){
        list.stream().peek(e->e.setName(e.getName()+"^-^")).collect(Collectors.toList()).forEach(System.out::println);
    }

    /**
     * 依据equal和hashcode去重
     * */
    public static void distinct(){
        List<Integer> list=Arrays.asList(1,2,3,4,3,2,4);
        list.stream().distinct().collect(Collectors.toList()).forEach(System.out::println);
    }

    /**
     * 截断
     * */
    public static void limit(List<User> list){
        list.stream().limit(2).collect(Collectors.toList()).forEach(System.out::println);
    }

    /**
     * 按照规则排序
     * */
    public static void sortByRule(List<User> list){
        list.stream().sorted((o1,o2)->Integer.parseInt(o2.getId())-Integer.parseInt(o1.getId())).collect(Collectors.toList()).forEach(System.out::println);
    }

    /**
     * 全部匹配
     * */
    public static void allMatch(List<User> list){
        boolean flg=list.stream().allMatch(user -> user.getClassName().equals("1"));
        System.out.println(flg);
    }

    /**
     * 有一个匹配
     * */
    public static void anyMatch(List<User> list){
        boolean flg=list.stream().anyMatch(user -> user.getClassName().equals("1"));
        System.out.println(flg);
    }

    /**
     * 都不匹配
     * */
    public static void noMatch(List<User> list){
        boolean flg=list.stream().noneMatch(user -> user.getClassName().equals("1"));
        System.out.println(flg);
    }

    /**
     * 返回第一个元素
     * */
    public static void findFirst(List<User> list){
        Optional<User> first = list.stream().findFirst();
        if(first.isPresent()){
            User user=first.get();
            System.out.println(user);
        }
    }

    /**
     * 返回任一元素
     * */
    public static void findAny(List<User> list){
        Optional<User> any = list.stream().findAny();
        if (any.isPresent()) {
            User user = any.get();
            System.out.println(user);
        }
    }

    /**
     * 流中元素总个数
     * */
    public static void count(List<User> list){
        Long count=list.stream().count();
        System.out.println(count);
    }

    /**
     * 流中最大值
     * */
    public static void max(List<User> list){
        User user=list.stream().max(((o1, o2) -> Integer.parseInt(o1.getId())-Integer.parseInt(o2.getId()))).get();
        System.out.println(user);
    }

    /**
     * 流中最小值
     * */
    public static void min(List<User> list){
        User user=list.stream().min(((o1, o2) -> Integer.parseInt(o1.getId())-Integer.parseInt(o2.getId()))).get();
        System.out.println(user);
    }
}
